<!--/**
* @author xuyh0817
* @email xuyh0817@foxmail.com
* @date 2022-02-16 15:58
* @version 2.0.0
* @doc todo
* @website todo
* @remark  如果要分发源码，需在本文件顶部保留此文件头信息！！
*/
-->
<template>
  <a-checkbox-group
    v-bind="attrs"
    :value="currentValue"
    :name="checkBoxName"
    @change="checkBoxChangeHandler"
  >
    <a-checkbox
      v-for="(item,index) in children"
      :key="index"
      v-bind="item"
    >
      {{ item.label }}
    </a-checkbox>
  </a-checkbox-group>
</template>

<script>
import {COMPONENT_TYPE, CUSTOM_EVENT_TYPE} from '../../constants'
import ComponentMixin from '../../XRenderMixins/XRenderComponentMixin'

export default {
  name: COMPONENT_TYPE.CHECK_BOX,
  mixins: [ComponentMixin],
  props: {
    value: {
      type: [Array, String]
    }
  },
  computed: {
    currentValue: {
      get() {
        const {value, formItem, isDebug} = this
        if (!(value instanceof Array) && typeof (value) !== 'string') {
          isDebug ? console.warn(`${formItem.prop} value类型错误,非String或Array`) : ''
          return []
        }
        if (value instanceof Array) {
          return value || []
        }
        return value.split(',') || []
      }
    },
    children: {
      get() {
        const {attrs, formItem, isDebug} = this
        const children = attrs.children
        if (typeof (children) !== 'string' && !(children instanceof Array)) {
          isDebug ? console.warn(`${formItem.prop} children类型错误,非String或Array`) : ''
          return []
        }
        if (children instanceof Array) {
          return children || []
        }
        return (new Function('return ' + children))()
      }
    },
    checkBoxName: {
      get() {
        const {attrs, formItem} = this
        if (!!attrs && !!attrs.name) {
          return attrs.name + Date.now()
        }
        if (!!formItem && !!formItem.prop) {
          return formItem.prop + Date.now()
        }
        return Date.now() + ''
      }
    }
  },
  methods: {
    checkBoxChangeHandler(value) {
      const {attrs, formItem, children} = this
      // 为关联的键进行赋值
      const obj = {}
      if (!!this.attrs && !!this.attrs.associations) {
        // 关联的key
        const associations = this.attrs.associations
        for (const child of children) {
          if (value.includes(child.value)) {
            for (const association of associations) {
              if (!!association.key && !!association.mapKey) {
                obj[association.key] = child[association.mapKey]
              }
            }
          }
        }

        this.$emit(CUSTOM_EVENT_TYPE.CUSTOM_EVENT, {
          type: CUSTOM_EVENT_TYPE.ASSOCIATION_CHANGE_EVENT,
          compType: COMPONENT_TYPE.CHECK_BOX,
          prop: formItem ? formItem.prop : '',
          value: obj,
          attrs: attrs,
          formItem: formItem
        })
      }

      this.$emit(CUSTOM_EVENT_TYPE.CHANGE, {
        type: COMPONENT_TYPE.CHECK_BOX,
        prop: formItem ? formItem.prop : '',
        value,
        attrs: attrs,
        formItem: formItem,
        association: obj
      })
    }
  }
}
</script>

<style scoped>

</style>
